package com.xbs.dao.enterprise;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xbs.dao.conn.BaseDao;
import com.xbs.dao.conn.RowDatas;
import com.xbs.entity.EnterpriseOrderEntity;
import com.xbs.entity.EpOrderAdminEntity;
import com.xbs.entity.EpOrderAdminQueryEntity;
import com.xbs.utils.PageUtilEntity;

public class EpOrderQueryAdminDao extends BaseDao{
	/*
	 * 根据EpOrderAdminQueryEntity查询企业订单
	 */
	public List<EpOrderAdminEntity> getOrderByQuery(EpOrderAdminQueryEntity e,int curr,int size){
		String sql="select m.eo_order_id,eo_account,eo_order_status,eo_rent_type,eo_reserve_get_time,eo_fact_return_time,rent_money*car_model_number as cash"
				+ " from tb_enterprise_order o,tb_enterprise_model m"
				+ " where o.eo_order_id=m.eo_order_id ";
		StringBuffer bf=new StringBuffer(sql);
		if(!e.getStatus().equals("全部")){
			bf.append("and eo_order_status='").append(e.getStatus()).append("'");
		}
		if(!((e.getStartTime()==null)||e.getStartTime().equals(""))){
			bf.append(" and eo_reserve_get_time>=").append(e.getStartTime());
		}
		if(!((e.getEndTime()==null)||e.getEndTime().equals(""))){
			bf.append(" and eo_reserve_get_time<=").append(e.getStartTime());
		}
		if(!(e.getOrderType()==2)){
			bf.append(" and eo_rent_type=").append(e.getOrderType());
		}
		if(!e.getKeyWord().equals("查询")){
			bf.append(" and eo_account like '%").append(e.getKeyWord()).append("%'");
		}
		bf.append(" group by m.eo_order_id").append(" limit ").append((curr-1)*size).append(",").append(size);
		/*group by m.eo_order_id*/
		RowDatas<EpOrderAdminEntity> rd=new RowDatas<EpOrderAdminEntity>() {

			@Override
			public EpOrderAdminEntity getRowData(ResultSet rs) throws SQLException {
				EpOrderAdminEntity ea=new EpOrderAdminEntity();
				ea.setEo_account(rs.getString("eo_account"));
				ea.setEo_fact_return_time(rs.getDate("eo_fact_return_time"));
				ea.setEo_reserve_get_time(rs.getDate("eo_reserve_get_time"));
				ea.setEo_order_id(rs.getString("eo_order_id"));
				ea.setEo_order_status(rs.getString("eo_order_status"));
				ea.setEo_rent_type(rs.getInt("eo_rent_type"));
				ea.setEo_plan_money(rs.getDouble("cash"));
				return ea;
			}
		};
		System.out.println(bf);
		return queryList(bf.toString(), rd);
	}
	/**
	 * 获取总的记录数
	 */
	public Integer getSum(EpOrderAdminQueryEntity e){
		String sql="select count(*) as sum from (select count(*)"
				+ " from tb_enterprise_order o,tb_enterprise_model m"
				+ " where o.eo_order_id=m.eo_order_id ";
		StringBuffer bf=new StringBuffer(sql);
		if(!e.getStatus().equals("全部")){
			bf.append("and eo_order_status='").append(e.getStatus()).append("'");
		}
		if(!((e.getStartTime()==null)||e.getStartTime().equals(""))){
			bf.append(" and eo_reserve_get_time>=").append(e.getStartTime());
		}
		if(!((e.getEndTime()==null)||e.getEndTime().equals(""))){
			bf.append(" and eo_reserve_get_time<=").append(e.getStartTime());
		}
		if(!(e.getOrderType()==2)){
			bf.append(" and eo_rent_type=").append(e.getOrderType());
		}
		if(!e.getKeyWord().equals("查询")){
			bf.append(" and eo_account like '%").append(e.getKeyWord()).append("%'");
		}
		
		
		RowDatas<Integer> rd=new RowDatas<Integer>() {

			@Override
			public Integer getRowData(ResultSet rs) throws SQLException {
				return rs.getInt("sum");
			}
		};
		bf.append(" group by m.eo_order_id) a");
		System.out.println(bf);
		return query(bf.toString(), rd);
	}
}
